// scripts/gen-free-fontawesome.js
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import yaml from 'js-yaml';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const metadataPath = path.resolve(__dirname, '../node_modules/@fortawesome/fontawesome-free/metadata/icons.yml');

if (!fs.existsSync(metadataPath)) {
  console.error('❌ 找不到 metadata/icons.yml');
  process.exit(1);
}

const fileContent = fs.readFileSync(metadataPath, 'utf-8');
const metadata = yaml.load(fileContent);

const icons = [];

for (const iconName in metadata) {
  const info = metadata[iconName];
  if (info.styles && Array.isArray(info.styles)) {
    info.styles.forEach(style => {
      let prefix = '';
      switch (style) {
        case 'solid':
          prefix = 'fas';
          break;
        case 'regular':
          prefix = 'far';
          break;
        case 'brands':
          prefix = 'fab';
          break;
        default:
          prefix = `fa-${style}`;
      }
      icons.push(`${prefix} fa-${iconName}`);
    });
  }
}

const unique = Array.from(new Set(icons)).sort();
const outPath = path.resolve(__dirname, '../free-icons.json');
fs.writeFileSync(outPath, JSON.stringify(unique, null, 2), 'utf-8');

console.log(`✅ 生成成功：${outPath}`);
console.log(`📦 共 ${unique.length} 个免费 Font Awesome 图标类名`);
